local wu__wangyou = fk.CreateSkill{
  name = "wu__wangyou",
}

Fk:loadTranslationTable{
  ["wu__wangyou"] = "忘忧",
  [":wu__wangyou"] = "出牌阶段限一次，可以记录你的手牌数。若如此做，本阶段下一次有牌置入弃牌堆时，你将手牌数调整至与记录相同。",
  ["#wu__wangyou-active"] = "忘忧：记录你的手牌数，然后本阶段下一次有牌置入弃牌堆时，你将手牌数调整至与记录相同",
}

wu__wangyou:addEffect("active", {
  anim_type = "control",
  prompt = "#wu__wangyou-active",
  max_phase_use_time = 1,
  phase = "play",
  card_filter = Util.FalseFunc,
  target_filter = Util.FalseFunc,

  can_use = function(self, player)
    return player:usedSkillTimes(wu__wangyou.name, Player.HistoryPhase) < 1
  end,

  on_use = function(self, room, effect)
    local player = effect.from
    local current_handnum = player:getHandcardNum()

    room:setPlayerMark(player, "wangyou_record", current_handnum)
    room:setPlayerMark(player, "wangyou_ready", 1)

    return false
  end,
})

wu__wangyou:addEffect(fk.AfterCardsMove, {
  anim_type = "drawcard",

  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(wu__wangyou.name) and player:getMark("wangyou_ready") == 1 then 
      for _, move in ipairs(data) do
        if move.toArea == Card.DiscardPile then
          return true
        end
      end
      return false
    end
  end,

  on_cost = Util.TrueFunc,

  on_use = function(self, event, target, player, data)
    local room = player.room
    local record_num = player:getMark("wangyou_record")
    local current_num = player:getHandcardNum()
    local hand_diff = current_num - record_num

    if hand_diff > 0 then
      room:askToDiscard(player, {
        min_num = hand_diff,
        max_num = hand_diff,
        include_equip = false,
        skill_name = wu__wangyou.name,
        cancelable = false,
      })
    elseif hand_diff < 0 then
      local draw_num = -hand_diff
      player:drawCards(draw_num, wu__wangyou.name)
    end

    room:setPlayerMark(player, "wangyou_record", 0)
    room:setPlayerMark(player, "wangyou_ready", 0)
  end,
})

wu__wangyou:addEffect(fk.EventPhaseEnd, {
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(wu__wangyou.name)
      and data.phase == Player.Play
      and (player:getMark("wangyou_ready") == 1 or player:getMark("wangyou_record") > 0)
  end,

  on_use = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "wangyou_record", 0)
    player.room:setPlayerMark(player, "wangyou_ready", 0)
  end,
})

return wu__wangyou